package com.seabook.store.auth.service;

import com.seabook.store.auth.dao.UserDao;

import com.seabook.store.auth.domain.Address;
import com.seabook.store.auth.domain.User;

public class UserServiceImpl implements UserService {
    
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public User login(String userName, String password) {
        return userDao.checkUser(userName, password);
    }

    public boolean checkUserName(String userName) {
        return userDao.validateUserName(userName);
    }

    public void saveUser(User user) {
        Address addr= user.getDefaultAddress();
        addr.setDefaultR(true);
        addr.setUserId(user.getUserId());
        user.setUserId(userDao.saveUser(user));
        if(addr.getId() > 0)
            userDao.updateAddress(addr);
        else{
            user.setDefaultReceiver(userDao.addAddress(addr));
        }
    }

    public User createUser(String userName, String password) {
        int userId = userDao.createUser(userName, password);
        User user = new User();
        user.setUserId(userId);
        user.setUserName(userName);
        return user;
    }

    public User getUserInfo(User user) {
        // TODO Auto-generated method stub
        user = userDao.getUserDetail(user);
        user.setReceiver(userDao.getAddress(user.getUserId()));
        return user;
    }

    public Address addAddress(Address address) {
        return userDao.addAddress(address);
    }

    public void changePassword(int userId, String password) {
       userDao.changePassword(userId, password);       
    }
}
